﻿using Subkismet.Services.GoogleSafeBrowsing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Subkismet;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Configuration;

namespace UnitTests.GoogleSafeBrowsing
{


    /// <summary>
    ///This is a test class for GoogleSafeBrowsingTest and is intended
    ///to contain all GoogleSafeBrowsingTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GoogleSafeBrowsingTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for UpdateList
        ///</summary>
        [TestMethod()]
        public void UpdateListTest()
        {
            Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing target =
                new Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing();

            BlackListType listType = BlackListType.Phishing;

            bool expected = true;
            bool actual;

            actual = target.UpdateList(listType);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CheckPost
        ///</summary>
        [TestMethod()]
        public void CheckPostTest()
        {
            Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing target =
                new Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing();

            Comment comment = new Comment(IPAddress.Parse("192.168.0.1"), "Not required!");
            comment.Content = File.ReadAllText
                (ConfigurationManager.AppSettings["basePath"] + "SampleText.txt");

            int numberOfPhishingOccurs = 0;
            int numberOfPhishingOccursExpected = 0;
            int numberOfMalwareOccurs = 0;
            int numberOfMalwareOccursExpected = 0;

            bool expected = false;
            bool actual;

            actual = target.CheckPost(comment, out numberOfPhishingOccurs, out numberOfMalwareOccurs);

            Assert.AreEqual(numberOfPhishingOccursExpected, numberOfPhishingOccurs);
            Assert.AreEqual(numberOfMalwareOccursExpected, numberOfMalwareOccurs);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CheckLink
        ///</summary>
        [TestMethod()]
        public void CheckLinkTest()
        {
            Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing target =
                new Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing();

            string link = "http://allkindsofbdsm.com/";

            CheckResultType expected = CheckResultType.Safe;
            CheckResultType actual;

            actual = target.CheckLink(link);

            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CheckLinks
        ///</summary>
        [TestMethod()]
        public void CheckLinksTest()
        {
            Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing target =
                new Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing();

            List<string> links = new List<string>();
            links.Add("http://google.com");
            links.Add("http://subkismet.com");
            links.Add("http://blogml.org");

            int numberOfPhishingOccurs = 0;
            int numberOfPhishingOccursExpected = 0;
            int numberOfMalwareOccurs = 0;
            int numberOfMalwareOccursExpected = 0;

            bool expected = false;
            bool actual;

            actual = target.CheckLinks(links, out numberOfPhishingOccurs, out numberOfMalwareOccurs);

            Assert.AreEqual(numberOfPhishingOccursExpected, numberOfPhishingOccurs);
            Assert.AreEqual(numberOfMalwareOccursExpected, numberOfMalwareOccurs);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetLinksFromText
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Subkismet.dll")]
        public void GetLinksFromTextTest()
        {
            GoogleSafeBrowsing_Accessor target = new GoogleSafeBrowsing_Accessor();

            string text = @"<a href=""http://google.com"">test2</a>...<a href=""http://subkismet.com"">test2</a>";

            List<string> expected = new List<string>();

            expected.Add("http://google.com");
            expected.Add("http://subkismet.com");

            List<string> actual;

            actual = target.GetLinksFromText(text);

            TestGeneric<string>(actual, expected);
        }

        /// <summary>
        ///A test for GetUserAgent
        ///</summary>
        [TestMethod()]
        public void GetUserAgentTest()
        {
            string applicationName = "Subkismet";

            string expected =
                "Subkismet, Google Safe Browsing API, Implementation for .NET in Subkismet, Version: 0.2.0.0";

            string actual;
            actual = Subkismet.Services.GoogleSafeBrowsing.GoogleSafeBrowsing.GetUserAgent(applicationName);

            Assert.AreEqual(expected, actual);
        }

        #region Helper methods

        private void TestGeneric<T>(List<T> actual, List<T> expected)
        {
            IEnumerator<T> actualEnumerator = actual.GetEnumerator();

            for (int i = 0; i < expected.Count; i++)
            {
                Assert.IsTrue(actualEnumerator.MoveNext());
                Assert.IsTrue(actualEnumerator.Current.Equals(expected[i]));
            }

            Assert.IsFalse(actualEnumerator.MoveNext());
        }

        #endregion
    }
}
